/*
 * UserInfo.java
 *
 * Created on 25 Март 2008 г., 19:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chatty.search;

import DrawControls.icons.*;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;

import chatty.*;
import chatty.comm.StringConvertor;
import chatty.comm.Util;
import chatty.forms.*;
import chatty.ui.*;
import chatty.ui.menu.*;
import chatty.ui.base.*;
import chatty.modules.*;
import chatty.modules.fs.*;
import chatty.modules.photo.*;
import chatty.util.ResBundle;

import protocol.*;
import protocol.simp.*;

/**
 *
 * @author vladimir
 */
public class UserInfo implements SelectListener {
    private final Protocol protocol;
    private TextListEx profileView;
    
    /** Creates a new instance of UserInfo */
    public UserInfo(Protocol prot, String uin) {
        protocol = prot;
        realUin = uin;
    }
    public UserInfo(Protocol prot) {
        protocol = prot;
        realUin = null;
    }
    public void setProfileView(TextListEx view) {
        profileView = view;
    }
    public TextListEx getProfileView() {
        return profileView;
    }

    
    private static final int INFO_MENU_COPY     = 1040;
	private static final int INFO_MENU_COPY_ADD = 1041;
    private static final int INFO_MENU_COPY_ALL = 1042;
    private static final int INFO_MENU_BACK     = 1043;
    private static final int INFO_MENU_AVATAR   = 1044;
	private static final int INFO_MENU_UPDATE_NICK   = 1045;
    private static final int INFO_MENU_EDIT     	 = 1046;
    private static final int INFO_MENU_REMOVE_AVATAR = 1047;
    private static final int INFO_MENU_ADD_AVATAR    = 1048;
    private static final int INFO_MENU_TAKE_AVATAR   = 1049;
    
    public void setOptimalName() {
        Contact contact = protocol.getItemByUIN(uin);
        if (null != contact) {
            contact.setOptimalName(this);
        }
    }
    public synchronized void updateProfileView() {
        // #sijapp cond.if modules_DEBUGLOG is "true" #
        if ((null == profileView)) {
            chatty.modules.DebugLog.panic("profileView is null");
            return;
        }
        // #sijapp cond.end#
        profileView.lock();
        profileView.clear();
        
        profileView.setHeader("main_info");
        profileView.addParam(protocol.getUinName(),    uin);
        profileView.addParam("nick",   nick);
        profileView.addParam("name", getName());
        profileView.addParam("gender", getGenderAsString());
        if (0 < age) {
            profileView.addParam("age", Integer.toString(age));
        }
        profileView.addParam("email",  email);
        if (auth) {
            profileView.addParam("auth", ResBundle.getString("yes"));
        }
        profileView.addParam("birth_day",  birthDay);
        profileView.addParam("cell_phone", cellPhone);
        profileView.addParam("home_page",  homePage);
        profileView.addParam("interests",  interests);
        profileView.addParam("notes",      about);
        
        profileView.setHeader("home_info");
        profileView.addParam("addr",  homeAddress);
        profileView.addParam("city",  homeCity);
        profileView.addParam("state", homeState);
        profileView.addParam("phone", homePhones);
        profileView.addParam("fax",   homeFax);
        profileView.addParam("tz",    homeTz);
		
        profileView.setHeader("work_info");
        profileView.addParam("title",    workCompany);
        profileView.addParam("depart",   workDepartment);
        profileView.addParam("position", workPosition);
        profileView.addParam("addr",     workAddress);
        profileView.addParam("city",     workCity);
        profileView.addParam("state",    workState);
        profileView.addParam("phone",    workPhone);
        profileView.addParam("fax",      workFax);
        profileView.addParam("web_page", workWebPage);
		
        profileView.setHeader("avatar");
        profileView.addParamImage(null, avatar);

        profileView.unlock();

        //profileView.setCaption(getName());
        MenuModel menu = new MenuModel(MenuModel.MENU_LEFT);
        menu.addItem("copy_text",     Chatty.chatIcons.iconAt(Chatty.ICON_COPY), 	INFO_MENU_COPY);
		menu.addItem("copy_add",      Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ADD),INFO_MENU_COPY_ADD);
        menu.addItem("copy_all_text", Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ALL),INFO_MENU_COPY_ALL);
        if (isEditable()) {
            menu.addItem("edit",      Chatty.icons.iconAt(Chatty.ICON_EDIT),        INFO_MENU_EDIT);
        }
		if (null != uin) {
			menu.addItem("get_avatar",Chatty.icons.iconAt(Chatty.ICON_AVATAR),      INFO_MENU_AVATAR);
        }
		menu.addItem("update_nick",   Chatty.icons.iconAt(Chatty.ICON_USER_RENAME),        INFO_MENU_UPDATE_NICK);
        menu.addItem("back",          Chatty.icons.iconAt(Chatty.ICON_BACK), INFO_MENU_BACK);
        menu.setActionListener(this);
        profileView.setMenu(menu, INFO_MENU_BACK, INFO_MENU_COPY);
    }
	public void setProfileViewToWait() {
        MenuModel menu = new MenuModel(MenuModel.MENU_LEFT);
        menu.addItem("back", Chatty.icons.iconAt(Chatty.ICON_BACK),	 INFO_MENU_BACK);
        profileView.setInfoMessage(ResBundle.getString("wait"));
        menu.setActionListener(this);
        profileView.setMenu(menu, INFO_MENU_BACK, INFO_MENU_COPY);
    }
	
    public boolean isEditable() {
            return protocol.getUserId().equals(uin) && protocol.isConnected();
    }
    
    public void select(Select select, MenuModel model, int cmd) {
        switch (cmd) {
            case INFO_MENU_COPY:
            case INFO_MENU_COPY_ALL:
                profileView.copy(INFO_MENU_COPY_ALL == cmd);
                profileView.restore();
                break;

            case INFO_MENU_COPY_ADD:
                profileView.copy_add();
                profileView.restore();
                break;
				
            case INFO_MENU_BACK:
                profileView.back();
                profileView.clear();
                break;

			case INFO_MENU_UPDATE_NICK:
				Contact contact = protocol.getItemByUIN(uin);
				protocol.renameContact(contact, nick);
				break;
            case INFO_MENU_AVATAR:
                protocol.getAvatar(this);
                profileView.restore();
                break;

            case INFO_MENU_EDIT:
                new EditInfo(protocol, this).show();
                break;
				
            // #sijapp cond.if modules_FILES="true"#
            // #sijapp cond.if target is "MIDP2" #
            case INFO_MENU_TAKE_AVATAR:
               // ViewFinder vf = new ViewFinder();
               // vf.setPhotoListener(this);
               //vf.show();
                break;
            // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" #
            case INFO_MENU_REMOVE_AVATAR:
                //removeAvatar();
                protocol.saveUserInfo(this);
                updateProfileView();
                profileView.restore();
                break;

            case INFO_MENU_ADD_AVATAR:
                //FileBrowser fsBrowser = new FileBrowser(false);
                //fsBrowser.setListener(this);
                //fsBrowser.activate();
                break;
            // #sijapp cond.end #
            // #sijapp cond.end #
            // #sijapp cond.end #
        }
    }
    public final String realUin;

    public String uin;
    public String nick;
    public String email;
    public String homeCity;
    public String firstName;
    public String lastName;

    public String homeState;
    public String homePhones;
    public String homeFax;
    public String homeAddress;
	public String homeTz;
    public String cellPhone;

    public String homePage;
    public String interests;
    
    public String about;

    public String workCity;
    public String workState;
    public String workPhone;
    public String workFax;
    public String workAddress;
    public String workCompany;
    public String workDepartment;
    public String workPosition;
	public String workWebPage;
    public String birthDay;
    
    public int age;
    public byte gender;
    //public String auth;
    public boolean auth; // required

    // Convert gender code to string
    public String getGenderAsString() {
        switch (gender) {
            case 1: return ResBundle.getString("female");
            case 2: return ResBundle.getString("male");
        }
        return "";
    }

    private String packString(String str) {
        return (null == str) ? "" : str.trim();
    }
    public String getName() {
        return packString(packString(firstName) + " " + packString(lastName));
    }
    public String getOptimalName() {
        String optimalName = packString(nick);
        if (optimalName.length() == 0) {
            optimalName = packString(getName());
        }
        if (optimalName.length() == 0) {
            optimalName = packString(firstName);
        }
        if (optimalName.length() == 0) {
            optimalName = packString(lastName);
        }
        return optimalName;
    }
    
    public Icon avatar;
    public void setAvatar(Image img) {
        if (null == img) {
            avatar = null;
            return;
        }
        int height = NativeCanvas.getScreenHeight() * 2 / 3;
        int width = NativeCanvas.getScreenWidth() - 5;
        Image image = Util.createThumbnail(img, width, height);
        avatar = new Icon(image, 0, 0, image.getWidth(), image.getHeight());
    }
	
	public void setAvatar(byte[] data) {
			setAvatar(Image.createImage(data, 0, data.length));      
    }
}
